
package gui.std;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;

import util.Constants;
import app.User;

@SuppressWarnings("serial")
public class GUserItem extends JPanel {
	
	public GUserItem(User u) {
		user = u;
		initialize();
				
		new Thread() {
			public void run() {
				try {
					while (true) {
						if (blink) {
							img.setBackground(Constants.BLINK_COLOR);
							sleep(500);
							img.setBackground(Constants.DEFAULT_BACKGROUND_ITEM_COLOR);
						}
						sleep(500);
					}
				}
				catch (InterruptedException e) {interrupt();}
			}
		}.start();
	}
	
	private void initialize() {
		setLayout(new BorderLayout());

		img = new JButton(String.format("%-35s", user.getNick()), user.getImage());
		img.setName(""+user.getId());
		img.setFocusable(false);
		img.setBackground(Constants.DEFAULT_BACKGROUND_ITEM_COLOR);
		add(img);
		
		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (display != null) {
					display.removeAll();
					display.add(new GUserInfo(user));
					display.updateUI();
				}
			}
		});
		addMouseListener(new MouseAdapter (){
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				img.doClick();
			}
		});
	}
	
	public User getUser() {
		return user;
	}
	
	public void setDisplay(JPanel d) {
		display = d;
	} 
	
	public void setBlink(boolean b) {
		blink = b;
	}
	
	public void mark() {
		img.setForeground(Color.GRAY);
	}
	
	public void addActionListener(ActionListener e) {
		img.addActionListener(e);
	}
		
	private User user;
	private JButton img;
	private JPanel display;
	private boolean blink;
}
